package com.bt.lms.ui.page.teaching.supervise;

import com.bt.lms.ui.action.teaching.supervise.LookStuBtnAction;
import com.bt.lms.ui.action.teaching.supervise.PageTeachingSuperviseLookCourseResourceBtnAction;
import com.bt.lms.ui.page.sys.utils.MyHBox;
import com.bt.lms.ui.page.sys.utils.MyVBox;
import com.bt.lms.ui.page.teaching.supervise.action.RealTimeClassBtn;
import com.bt.lms.ui.page.teaching.supervise.action.TeachingCheckBtn;
import com.bt.lms.ui.page.teaching.supervise.action.AddRealTimeClassAction;
import com.bt.lms.ui.vo.PageSkipInfo;
import com.bt.lms.ui.page.teaching.supervise.action.GradeBtn;
import com.bt.lms.ui.page.sys.vo.RealTimeClassInfo;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; 
import java.util.ArrayList;
import java.util.List;

/**
 * 实时课堂列表界面
 */
public class PageRealTimeClassList extends VBox {

    /**
     * 界面跳转数据
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 构建课堂列表界面
     * @param pageSkipInfo 界面跳转数据
     */
    public PageRealTimeClassList(PageSkipInfo pageSkipInfo) {

        //自己可能用的东西
        this.setId("PageRealTimeClassList");
        this.pageSkipInfo = pageSkipInfo;

        //大小
        int wight = 1280;
        this.setPrefWidth(wight);
        this.setPrefHeight(650);

        //布局
        Insets insets = new Insets(0);
        this.setPadding(insets);

        //组件
        //定位区域
        MyHBox locationHBox = new MyHBox();
        locationHBoxInit(locationHBox);
        this.getChildren().add(locationHBox);

        //按钮区域
        MyHBox btnHBox = new MyHBox();
        initButtonHBox(btnHBox);
        this.getChildren().add(btnHBox);

        //列表区域
        MyVBox listVBox = new MyVBox();
        listVBoxInit(listVBox);
        this.getChildren().add(listVBox);

        //搜索区域
        MyHBox searchHBox = new MyHBox();
        searchHBoxInit(searchHBox);
        this.getChildren().add(searchHBox);
    }

    /**
     * 搜索区域初始化
     *
     * @param searchHBox 搜索区域
     */
    private void searchHBoxInit(   HBox searchHBox) {

        //数据控制
        int width = 45;
        int sumStrip = 100;//总条数
        int present = 10;//当前页数
        String sumStripStr = "总记录数" + sumStrip + "条";
        String presentStr = "/" + present;

        //布局
        Insets insets = new Insets(10);
        searchHBox.setPadding(insets);
        searchHBox.setSpacing(10);
        searchHBox.setAlignment(Pos.CENTER_RIGHT);

        //组件
        Label firstLab = new Label("« 首页");
        searchHBox.getChildren().add(firstLab);
        Label lastLab = new Label("« 上一页");
        searchHBox.getChildren().add(lastLab);
        Button pageOne = new Button("1");
        searchHBox.getChildren().add(pageOne);
        Button pageTwo = new Button("2");
        searchHBox.getChildren().add(pageTwo);
        Button pageThree = new Button("3");
        searchHBox.getChildren().add(pageThree);
        Label nextLab = new Label("下一页 »");
        searchHBox.getChildren().add(nextLab);
        Label finallyLab = new Label("尾页 »");
        searchHBox.getChildren().add(finallyLab);
        Label sumNumberLab = new Label(sumStripStr);
        searchHBox.getChildren().add(sumNumberLab);
        TextField pageNumberTF = new TextField();
        pageNumberTF.setPrefWidth(width);
        searchHBox.getChildren().add(pageNumberTF);
        Label presentLab = new Label(presentStr);
        searchHBox.getChildren().add(presentLab);
        Button notarizeBtn = new Button("GO");
        searchHBox.getChildren().add(notarizeBtn);

    }

    /**
     * 列表区域初始化
     *
     * @param listVBox 列表区域
     */
    private void listVBoxInit(MyVBox listVBox) {

        //操作按钮初始化（新增值机安排）
        operateBtnInit(listVBox);

        //列表
        //1.建立列表视图
        TableView<RealTimeClassInfo> tableView = new TableView<>();

        //2.列头
        //2.1 新建列头RealTimeClassInfo
        TableColumn<RealTimeClassInfo, String> nameTC = new TableColumn<>("主题");
        TableColumn<RealTimeClassInfo, String> teacherNameTC = new TableColumn<>("主持人/主讲教师" );
        TableColumn<RealTimeClassInfo, String> timeTC = new TableColumn<>("实时课堂时间");
        TableColumn<RealTimeClassInfo, Integer> durationTC = new TableColumn<>("课程时长（分钟）");
        TableColumn<RealTimeClassInfo, String> perContrastTC = new TableColumn<>("实际参与人数/应参与人数");
        TableColumn<RealTimeClassInfo, String> stateTC = new TableColumn<>("状态");
        TableColumn<RealTimeClassInfo, HBox> operateTC = new TableColumn<>("操作");

        //2.2 列头添加到视图
        tableView.getColumns().addAll(nameTC,teacherNameTC,timeTC,durationTC,perContrastTC,stateTC,operateTC);

        //3.模拟数据
        //3.1 新建多个假数据
        List<RealTimeClassInfo> realTimeClassInfos = simulatedData();
        //3.2 格式转化(用户数据集合转换成Javafx组件集合)
        ObservableList<RealTimeClassInfo> users = FXCollections.observableArrayList(realTimeClassInfos);
        //3.3 数据添加到表格
        tableView.setItems(users);

        //4.映射（让每一列知道要展示的属性是哪一个）
        nameTC.setCellValueFactory(new PropertyValueFactory<>("name"));
        teacherNameTC.setCellValueFactory(new PropertyValueFactory<>("teacherName"));
        timeTC.setCellValueFactory(new PropertyValueFactory<>("time"));
        durationTC.setCellValueFactory(new PropertyValueFactory<>("duration"));
        perContrastTC.setCellValueFactory(new PropertyValueFactory<>("perContrast"));
        stateTC.setCellValueFactory(new PropertyValueFactory<>("status"));
        operateTC.setCellValueFactory(new PropertyValueFactory<>("operate"));

        //试图添加
        ObservableList<Node> tableChildren = listVBox.getChildren();
        tableChildren.add(tableView);

    }

    /**
     * 列表区域的操作按钮初始化
     *
     * @param listVBox 列表盒子
     */
    private void operateBtnInit(   MyVBox listVBox) {

        //组件(操作按钮)
        Button addRoleBtn = new Button("新增实时课堂");
        //事件
        AddRealTimeClassAction addRealTimeClassAction = new AddRealTimeClassAction(pageSkipInfo);
        addRoleBtn.setOnAction(addRealTimeClassAction);
        //按钮内部布局
        Insets addUserBtnInsets = new Insets(10);
        addRoleBtn.setPadding(addUserBtnInsets);
        //操作按钮盒子（让操作按钮有间隔）
        HBox addUserBtnBox = new HBox();
        //操作按钮盒子布局
        Insets addUserBtnBoxInsets = new Insets(10);
        addUserBtnBox.setPadding(addUserBtnBoxInsets);
        //盒子内添加按钮
        addUserBtnBox.getChildren().add(addRoleBtn);
        //添加到列表容器
        ObservableList<Node> addUserChildren = listVBox.getChildren();
        addUserChildren.add(addUserBtnBox);
    }


    /**
     * 定位区域初始化
     *
     * @param locationHBox 定位区域容器
     */
    private void locationHBoxInit(   MyHBox locationHBox) {

        //控制数据
        int height = 42;

        //大小
        locationHBox.setPrefHeight(height);

        //布局
        Insets insets = new Insets(5);
        locationHBox.setPadding(insets);
        locationHBox.setSpacing(0);
        locationHBox.setAlignment(Pos.CENTER_LEFT);

        //组件
        Label siteLab = new Label("当前位置：");
        Label oneCatalogLab = new Label("🏠>系统管理");
        Label twoCatalogLab = new Label(">实时课堂管理");

        //添加组件
        locationHBox.getChildren().addAll(siteLab, oneCatalogLab, twoCatalogLab);

    }

    /**
     * 列表界面模拟数据
     *
     * @return 模拟好的数据集合
     */
    private    List<RealTimeClassInfo> simulatedData() {

        //数据总集合
        List<RealTimeClassInfo> realTimeClassInfos = new ArrayList<>();

        //模拟第一条数据
        oneSimulateData(realTimeClassInfos);

        //模拟第二条数据
        twoSimulateData(realTimeClassInfos);

        //模拟第三条数据
        threeSimulateData(realTimeClassInfos);

        //模拟第四条数据
        fourSimulateData(realTimeClassInfos);

        //返回全部数据
        return realTimeClassInfos;
    }

    /**
     * 第四条数据模拟
     *
     * @param realTimeClassInfos 总数据集合
     */
    private void fourSimulateData(   List<RealTimeClassInfo> realTimeClassInfos) {

        //第四条
        //操作按钮盒子
        HBox operateHBox = operateHBoxInit(2);
        //界面数据对象
        RealTimeClassInfo teacherCheckInInfo = new RealTimeClassInfo();
        teacherCheckInInfo.setState("已取消");
        teacherCheckInInfo.setOperate(operateHBox);
        realTimeClassInfos.add(teacherCheckInInfo);
    }


    /**
     * 第三条数据模拟
     *
     * @param realTimeClassInfos 总数据集合
     */
    private void threeSimulateData(   List<RealTimeClassInfo> realTimeClassInfos) {

        //第三条
        //操作按钮盒子
        HBox operateHBox = operateHBoxInit(2);
        //界面数据对象
        RealTimeClassInfo teacherCheckInInfo = new RealTimeClassInfo();
        teacherCheckInInfo.setState("已过期");
        teacherCheckInInfo.setOperate(operateHBox);
        realTimeClassInfos.add(teacherCheckInInfo);
    }

    /**
     * 第二条数据模拟
     *
     * @param realTimeClassInfos 总数据集合
     */
    private void twoSimulateData(   List<RealTimeClassInfo> realTimeClassInfos) {

        //第二条
        //操作按钮盒子
        HBox operateHBox = operateHBoxInit(1);
        //界面数据对象
        RealTimeClassInfo teacherCheckInInfo = new RealTimeClassInfo();
        teacherCheckInInfo.setPerContrast("25/45");
        teacherCheckInInfo.setState("进行中");
        teacherCheckInInfo.setOperate(operateHBox);
        realTimeClassInfos.add(teacherCheckInInfo);
    }

    /**
     * 第一条数据模拟
     *
     * @param realTimeClassInfos 总数据集合
     */
    private void oneSimulateData(   List<RealTimeClassInfo> realTimeClassInfos) {

        //第一条
        //操作按钮盒子
        HBox operateHBox = operateHBoxInit(0);
        //界面数据对象
        RealTimeClassInfo teacherCheckInInfo = new RealTimeClassInfo("课程串讲", "张老师",
                "2023/05/04 10:00-12:00",120 ,"未开始",operateHBox);
        realTimeClassInfos.add(teacherCheckInInfo);
    }

    /**
     * 操作按钮盒子初始化
     * @return 操作按钮盒子
     */
    private    HBox operateHBoxInit(   Integer integer) {

        //新建操作盒子
        HBox operateHBox = new HBox();
        operateHBox.setPadding(new Insets(2));
        operateHBox.setSpacing(10);

        switch (integer){

            case 0:
                operateHBoxInitOne(operateHBox);
                break;
            case 1:
                operateHBoxInitTwo(operateHBox);
                break;
            case 2:
                operateHBoxInitThree(operateHBox);
                break;
            case 3:
                operateHBoxInitFour(operateHBox);
                break;
            default :
                break;
        }

        return operateHBox;
    }

    /**
     * 操作盒子初始化（已取消）
     * @param operateHBox 盒子
     */
    private void operateHBoxInitFour(   HBox operateHBox) {

        //下载课程资料
        Label downloadLab = new Label("下载课程资料");
        operateHBox.getChildren().add(downloadLab);
    }

    /**
     * 操作盒子初始化（已过期）
     * @param operateHBox 盒子
     */
    private void operateHBoxInitThree(   HBox operateHBox) {
        //下载课程资料
        Label downloadLab = new Label("下载课程资料");
        operateHBox.getChildren().add(downloadLab);

        //操作按钮(课程回放
        Label playbackLab = new Label("课程回放");
        operateHBox.getChildren().add(playbackLab);
    }

    /**
     * 操作盒子初始化（进行中）
     * @param operateHBox 盒子
     */
    private void operateHBoxInitTwo(   HBox operateHBox) {
        //下载课程资料
        Label downloadLab = new Label("下载课程资料");
        operateHBox.getChildren().add(downloadLab);
    }

    /**
     * 操作盒子初始化（未开始）
     * @param operateHBox 盒子
     */
    private void operateHBoxInitOne(   HBox operateHBox) {

        //下载课程资料
        Label downloadLab = new Label("下载课程资料");
        operateHBox.getChildren().add(downloadLab);

        //操作按钮(修改
        Label amendLab = new Label("修改");
        operateHBox.getChildren().add(amendLab);

        //操作按钮(取消
        Label cancelLab = new Label("取消");
        operateHBox.getChildren().add(cancelLab);

        //开启课堂
        Label openLab = new Label("开启课堂");
        operateHBox.getChildren().add(openLab);

    }


    /**
     * 按钮横向盒子
     *
     * @param buttonHBox 按钮盒子
     */
    private void initButtonHBox(HBox buttonHBox) {

        buttonHBox.setSpacing(20);
        buttonHBox.setPadding(new Insets(5));
        //查看学员按钮
        Button lookStuBtn = new Button("查看学员");
        LookStuBtnAction lookStuBtnAction = new LookStuBtnAction();
        lookStuBtn.setOnAction(lookStuBtnAction);

        //查看课程资源按钮
        Button lookCourseResourceBtn = new Button("查看课程资源");
        ObservableList<Node> children = buttonHBox.getChildren();
        children.addAll(lookStuBtn, lookCourseResourceBtn);
        //查看课程资源按钮事件
        PageTeachingSuperviseLookCourseResourceBtnAction pageTeachingSuperviseLookCourseResourceBtnAction = new PageTeachingSuperviseLookCourseResourceBtnAction();
        lookCourseResourceBtn.setOnAction(pageTeachingSuperviseLookCourseResourceBtnAction);

        //教师值机按钮
        Button teachingCheckBtn = new Button("教师值机管理");
        ObservableList<Node> teachingCheckBtnChildren = buttonHBox.getChildren();
        teachingCheckBtnChildren.add(teachingCheckBtn);
        //事件
        TeachingCheckBtn teachingCheckBtnAction = new TeachingCheckBtn();
        teachingCheckBtn.setOnAction(teachingCheckBtnAction);

        //实时课堂按钮
        Button realTimeClassBtn = new Button("实时课堂管理");
        ObservableList<Node> realTimeClassBtnChildren = buttonHBox.getChildren();
        realTimeClassBtnChildren.add(realTimeClassBtn);
        RealTimeClassBtn realTimeClassBtnAction = new RealTimeClassBtn();
        realTimeClassBtn.setOnAction(realTimeClassBtnAction);

        //成绩及结业按钮
        Button gradeBtn = new Button("成绩及结业管理");
        ObservableList<Node> gradeBtnChildren = buttonHBox.getChildren();
        gradeBtnChildren.add(gradeBtn);
        GradeBtn gradeBtnBtnAction = new GradeBtn();
        gradeBtn.setOnAction(gradeBtnBtnAction);

    }
}
